package com.HuangJiaQing.www.interceptor;
/**
 * @author HONOR
 * @date 2022/8/12 16:46
 */

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(value = "/*", dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD})
public class LoginFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }


    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        if (uri.contains("/toLogin") || uri.contains("/login") || uri.contains("/photo") || uri.contains("/js") || "/".equals(uri)
                || uri.contains("/selectGoods") || uri.contains("/goodsSearch")||uri.contains("regist")||uri.contains("toRegist")) {
            chain.doFilter(request, response);
        } else {
            HttpSession session = ((HttpServletRequest) request).getSession();
            Object user = session.getAttribute("user");
            Object marketers = session.getAttribute("marketers");
            Object courier = session.getAttribute("courier");
            Object admin = session.getAttribute("admin");
            if (user != null || marketers != null || courier != null || admin != null) {
                chain.doFilter(request, response);
            } else {
                req.getRequestDispatcher("/toLogin").forward(req, response);
            }

        }

    }
}
